﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

namespace PaintDotNetForms
{
    [XmlRoot(ElementName = "Konfiguracja")]
    public class Config
    {
        private string lang;
        [XmlElement(ElementName = "Lang")]
        public string Lang
        {
            get
            {
                return lang;
            }
            set
            {
                lang = value;
            }
        }
    }

    public class ConfigManager
    {
        private const string config_file = "paintnetcfg.xml";
        private string configPath = null;

        public ConfigManager()
        {
            configPath = string.Concat(Environment.GetEnvironmentVariable("USERPROFILE"), "\\.paintDotNetForms");
            if (!Directory.Exists(configPath))
                Directory.CreateDirectory(configPath);
            configPath = string.Concat(configPath, "\\", config_file);
        }

        public void Save(Config config)
        {
            XmlSerializer xmls = new XmlSerializer(typeof(Config));
            TextWriter tw = new StreamWriter(configPath);
            xmls.Serialize(tw, config);
            tw.Close();
        }


        public Config Config
        {
            get
            {
                XmlSerializer s = new XmlSerializer(typeof(Config));
                Config c = null;

                try
                {
                    TextReader r = new StreamReader(configPath);
                    c = (Config)s.Deserialize(r);
                    r.Close();

                }
                catch (Exception)
                {
                    Config defaultConfig = new Config();
                    defaultConfig.Lang = "pl";
                    Save(defaultConfig);
                    return defaultConfig;
                }

                return c;
            }
        }
    }
}